
# mp.fig-- runs a pipeline of editor commands (for automation)
# Copyright (C) 2021 mn
#
# This program is free software: you can redistribute it and/or modify 
#
# it under the terms of the GNU Affero General Public License as published by 
# the Free Software Foundation, either version 3 of the License, or 
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License 
# along with this program. If not, see <http://www.gnu.org/licenses/>.

function vget g k
python
    return g[k]
    fig
    fig

function vset g k v
python
    g[k] = v
    return g
    fig
    fig

function getglcmd gl
    now gl ltrim split now " " mid 1 1 return now
    fig

function gtoken g
    glcmd getglcmd g
    glen g len
    cmdlen glcmd len
    tlen instr g glcmd times -1 minus cmdlen plus glen
    token g right tlen return token
    fig

function textinsert buf p insp
    blen buf  len
    bef  buf  left insp
    brc  blen minus insp
    aft  arr  mid 1 0
    ifless insp blen
         aft  buf     right brc
         next
    buf  bef  plus p  plus aft
    now  return buf
    fig

function mp buf params
plugins arr mid 1 0
cmds arr mid 1 0 
cmdbuf arr mid 1 0
text arr mid 1 0
parsebuf ""
python
    try:
        osg = (os.getenv("HOME") + "/figured/").replace("//", "/")
        os.chdir(osg)
        from glob import glob
        plugins = glob("*.py")
    except:
        plugins = []
    import readline
    for each in plugins:
        try:
            sig = each.split(".")[0]
            if "." + sig not in cmds and sig[:7] != "figured":
                exec("from " + sig + " import " + sig)
                cmds += ["." + sig]
        except:
            pass            
    fig

    glen    vget params "g" ltrim  len
    tlen    ".mp"    len    times -1  plus glen
    cmdline vget params "g"        ltrim       right tlen  ltrim 
    m cmds print 
    forin pars cmdline
        now parsebuf plus pars swap now parsebuf
        forin cmdck cmds
              cklen cmdck len
              cl parsebuf lcase right cklen instr cl cmdck
              iftrue cl
                  plen parsebuf len minus cklen
                  prevlin parsebuf left plen
                  cmdbuf plus prevlin
                  now parsebuf right cklen swap now parsebuf
                  break
                  fig
              next        
        next
    cmdbuf plus parsebuf print 


    forin g cmdbuf
        
ck instr cmds ".gpreproc"
iftrue ck
python
    params["g"] = g
    g = gpreproc(buf, params)
    fig
    next

    gl     g             lcase    ltrim          rtrim
    glen   gl            len
    glcmd  getglcmd gl
    gld    glcmd         mid 1 1  instr gld "."  not

    gt gtoken g
    pb vset params "g" g  vset params "text" buf  vset params "token" gt
    params pb
    pad arr mid 1 0
    pb vset params "pad" pad
    params pb

    forin e cmds
        el e lcase
        ifequal glcmd el
insp 0
python
    insp = params["insp"]
    params["fquit"] = 0
    try: 
        print "BUF", buf
        print "params", params
        print "RUNNING text = " + e[1:] + "(buf, params)"
        exec("text = " + e[1:] + "(buf, params)")
        if type(text) == dict:
            params = text
            cksv = 1
            pad = params["pad"]
            if pad: buf = textinsert(buf, pad, insp) ; insp += len(pad) ; params["insp"] = insp
            else:   buf = params["text"] ; insp = params["insp"]
    except: pass
    fig
            next
        next

next

    params pb return params
    fig

